package com.springboot.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.springboot.mapper.StudentMapper;
import com.springboot.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Service(interfaceName = "com.springboot.service.StudentService",version = "1.0.0",timeout = 15000)

public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public Student queryStudentById(Integer id) {
        return studentMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer queryAllStudentCount() {

        //提高系统性能，用户体验提升
        //首先去redis缓存中查询，如果有直接使用，如果没有去数据库查询并放到redis缓存中
        Integer allStudentCount =(Integer)redisTemplate.opsForValue().get("allStudentCount");
        if(null==allStudentCount){
            //去数据库查询
            allStudentCount =studentMapper.selectAllStudentCount();
            //并存放到redis缓存中
            redisTemplate.opsForValue().set("allStudentCount",allStudentCount,30, TimeUnit.SECONDS);
        }
        return allStudentCount;
    }



}
